package com.dense.kuiniu.shelfcard.business;

import android.content.DialogInterface;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.LanguageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dense.kuiniu.shelfcard.MainActivity;
import com.dense.kuiniu.shelfcard.R;
import com.dense.kuiniu.shelfcard.base.BaseFragment;
import com.dense.kuiniu.shelfcard.base.ClickHelper;
import com.dense.kuiniu.shelfcard.base.Url;
import com.dense.kuiniu.shelfcard.business.handler.AddCardHandler;
import com.dense.kuiniu.shelfcard.business.handler.CardHandler;
import com.dense.kuiniu.shelfcard.business.handler.LostCardHandler;
import com.dense.kuiniu.shelfcard.card.idcard.CardReader;
import com.dense.kuiniu.shelfcard.card.idcard.IDCardInfo;
import com.dense.kuiniu.shelfcard.card.idcard.IDCardReadListener;
import com.dense.kuiniu.shelfcard.entity.Binding;
import com.dense.kuiniu.shelfcard.setting.SettingDelegate;
import com.dense.kuiniu.shelfcard.view.dialog.PutCardDialog;
import com.rxjava.rxlife.RxLife;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import rxhttp.RxHttp;


/**
 * @author Administrator
 * @date 2020/8/6
 */
public class HomeDelegate extends BaseFragment {

    @BindView(R.id.tv_device_number)
    TextView mTvDeviceNumber;
    private CardReader mCardReader;

    public static HomeDelegate getInstance() {
        return new HomeDelegate();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.delegate_home;
    }

    @Override
    protected void init() {
    }

    @OnClick(R.id.tv_title_name)
    public void onHighSetting(View view) {
        if (ClickHelper.click5Times()) {
            SettingDelegate delegate = findFragment(SettingDelegate.class);
            if (delegate == null) {
                delegate = SettingDelegate.getInstance();
            }
            start(delegate);
        }
    }

    @OnClick({R.id.tv_apply_card, R.id.tv_lost_card})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_apply_card:
                startReadIDCard(new AddCardHandler());
                break;
            case R.id.tv_lost_card:
                startReadIDCard(new LostCardHandler());
                break;
        }
    }

    @OnCheckedChanged({R.id.rb_chinese, R.id.rb_english})
    public void onchange(CompoundButton button, boolean isChecked) {
        if (button.getId() == R.id.rb_chinese) {
            if (isChecked) {
                if (!LanguageUtils.isAppliedLanguage(Locale.CHINA)) {
                    LanguageUtils.applyLanguage(Locale.CHINA, MainActivity.class);
                }
            }
        } else {
            if (isChecked) {
                if (!LanguageUtils.isAppliedLanguage(Locale.US)) {
                    LanguageUtils.applyLanguage(Locale.US, MainActivity.class);
                }
            }
        }
    }

    private void startReadIDCard(CardHandler handler) {
        if (mCardReader == null) {
            mCardReader = new CardReader(getContext());
        }
        PutCardDialog mPutCardDialog = new PutCardDialog(getContext());
        mPutCardDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mCardReader.cancelReadIDCard();
            }
        });
        mPutCardDialog.msg(getString(R.string.text_please_input_id_card)).show();
        mCardReader.readIdCardInfo(new IDCardReadListener() {
            @Override
            public void onSucceed(IDCardInfo info) {
                if (info != null) {
                    handler.handle(mPutCardDialog, HomeDelegate.this, info);
                }
            }

            @Override
            public void onError(String msg) {
                mPutCardDialog.error(msg);
            }
        });
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        queryLibrary();
    }

    private void queryLibrary() {
        RxHttp.get(Url.getBindingLibraryInfo)
                .add("deviceId", DeviceUtils.getAndroidID())
                .add("deviceType", "Android")
                .asResponse(Binding.class)
                .to(RxLife.toMain(this))  //感知生命周期，并在主线程回调
                .subscribe(binding -> {
                    if (mTvDeviceNumber != null) {
                        String deviceNumber = binding.getDeviceNum();
                        if (deviceNumber != null && !deviceNumber.isEmpty()) {
                            mTvDeviceNumber.setVisibility(View.VISIBLE);
                            mTvDeviceNumber.setText(binding.getDeviceNum());
                        } else {
                            mTvDeviceNumber.setVisibility(View.GONE);
                        }
                    }
                }, throwable -> {
                    ToastUtils.showShort(throwable.getMessage());
                });
    }


}